<template>
  <el-form :model="queryParams" ref="queryForm" size="default" :inline="true" label-width="68px">
    <el-form-item label="案件id" prop="caseId">
      <el-input
        v-model="queryParams.caseId"
        placeholder="请输入案件id"
        clearable
      />
    </el-form-item>
    <el-form-item label="借阅人" prop="borrower">
      <el-input
        v-model="queryParams.borrower"
        placeholder="请输入借阅人"
        clearable
      />
    </el-form-item>
    <el-form-item label="借阅时间" prop="borrowTime">
      <el-date-picker clearable
                      v-model="queryParams.borrowTime"
                      type="datetime"
                      format="YYYY-MM-DD HH:mm:ss"
                      value-format="YYYY-MM-DD HH:mm:ss"
                      placeholder="请选择借阅时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="归还时间" prop="returnTime">
      <el-date-picker clearable
                      v-model="queryParams.returnTime"
                      type="datetime"
                      format="YYYY-MM-DD HH:mm:ss"
                      value-format="YYYY-MM-DD HH:mm:ss"
                      placeholder="请选择归还时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      <el-button icon="Refresh" @click="resetQuery">重置</el-button>
    </el-form-item>
  </el-form>

  <el-row :gutter="10" class="mb8">
    <el-col :span="1.5">
      <el-button
        type="primary"
        plain
        icon="Plus"
        @click="handleAdd"
        v-perms="['archives:bt:add']"
        v-preventReClick
      >新增
      </el-button>
    </el-col>
  </el-row>

  <el-table v-loading="loading" :data="btList">
    <el-table-column label="主键id" align="center" prop="id" />
    <el-table-column label="案件id" align="center" prop="caseId" />
    <el-table-column label="借阅人" align="center" prop="borrower" />
    <el-table-column label="借阅时间" align="center" prop="borrowTime" width="180" />
    <el-table-column label="归还时间" align="center" prop="returnTime" width="180" />
    <el-table-column label="状态" align="center" prop="status">
      <template #default="scope">
        <status-tag :status="scope.row.status" :status-enum="statusOptions" />
      </template>
    </el-table-column>
    <el-table-column label="备注" align="center" prop="remark" />
    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
      <template #default="scope">
        <el-button
          type="primary"
          link
          icon="Edit"
          @click="handleUpdate(scope.row)"
          v-perms="['archives:bt:edit']"
          v-preventReClick
        >修改
        </el-button>
        <el-button
          type="primary"
          link
          icon="Delete"
          @click="handleDelete(scope.row)"
          v-perms="['archives:bt:remove']"
          v-preventReClick
        >删除
        </el-button>
        <el-button
          type="primary"
          link
          icon="More"
        >
          <el-dropdown trigger="click">
            <el-text type="primary">更多</el-text>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="handleCommand(scope.row,'materials')">案件材料</el-dropdown-item>
                <el-dropdown-item @click="handleCommand(scope.row,'report')">鉴定报告</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-pagination
    v-if="total>0"
    v-model:current-page="queryParams.pageNum"
    :page-size="10"
    layout="total, prev, pager, next"
    :total="total"
    style="float: right"
  />

  <!-- 添加或修改档案借阅与归还对话框 -->
  <el-dialog :title="title" v-model="open" width="500px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="案件id" prop="caseId">
        <el-input v-model="form.caseId" placeholder="请输入案件id" />
      </el-form-item>
      <el-form-item label="借阅人" prop="borrower">
        <el-input v-model="form.borrower" placeholder="请输入借阅人" />
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="form.status">
          <el-radio v-for="item in convertToOptions(statusOptions)" :key="item.value" size="large"
                    :value="item.value.toString()">{{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="借阅时间" prop="borrowTime">
        <el-date-picker clearable
                        v-model="form.borrowTime"
                        type="datetime"
                        format="YYYY-MM-DD HH:mm:ss"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="请选择借阅时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="归还时间" prop="returnTime">
        <el-date-picker clearable
                        v-model="form.returnTime"
                        type="datetime"
                        format="YYYY-MM-DD HH:mm:ss"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="请选择归还时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import StatusTag from '@/components/StatusTag.vue'
import { convertToOptions } from '@/utils/util.js'
import { BtService } from '@/api/archives/bt.js'

export default {
  name: 'BtIndex',
  components: { StatusTag },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 档案借阅与归还表格数据
      btList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        caseId: null,
        borrower: null,
        borrowTime: null,
        returnTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        caseId: [
          { required: true, message: '案件id不能为空', trigger: 'blur' }
        ],
        borrower: [
          { required: true, message: '借阅人不能为空', trigger: 'blur' }
        ],
        borrowTime: [
          { required: true, message: '借阅时间不能为空', trigger: 'blur' }
        ]
      },
      statusOptions: {
        '0': {
          type: 'primary',
          txt: '借出'
        },
        '1': {
          type: 'info',
          txt: '已归还'
        }
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    convertToOptions,
    /** 查询档案借阅与归还列表 */
    getList() {
      this.loading = true
      BtService.listBt(this.queryParams).then(response => {
        this.btList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        caseId: null,
        borrower: null,
        borrowTime: null,
        returnTime: null,
        status: '0',
        remark: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.title = '添加档案借阅与归还'
      this.reset()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      BtService.getBt(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改档案借阅与归还'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            BtService.updateBt(this.form).then(response => {
              this.$message.success('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            BtService.addBt(this.form).then(response => {
              this.$message.success('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除档案借阅与归还编号为"' + ids + '"的数据项？').then(function() {
        return BtService.delBt(ids)
      }).then(() => {
        this.getList()
        this.$message.success('删除成功')
      }).catch(() => {
      })
    },
    handleCommand(row, type) {
      if (type == 'materials') {
        this.$router.push({ path: '/case_identifications_materials', query: { id: row.id } })
      }
      if (type == 'report') {
        this.$router.push({ path: '/apprasial_reports', query: { id: row.id } })
      }
    }
  }
}
</script>
